<template>
  <div>
    <h1>品牌列表</h1>
    <el-form :inline="true" :model="pageFrom" class="demo-form-inline">
      <el-form-item label="品牌名称">
        <el-input
          v-model="pageFrom.keyWord"
          placeholder="请输入品牌名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadBrandList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="onAddBrand()">添加品牌</el-button>
    <el-button type="danger" @click="ondeleteBatchBrand()">删除</el-button>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="brandId" label="序号" />
      <el-table-column prop="brandName" label="品牌名称" />
      <el-table-column prop="brandUse" label="使用商品数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="brandStatus" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.brandStatus"
            :active-value="0"
            :inactive-value="1"
            active-text="显示"
            inactive-text="隐藏"
            @change="onUpdateStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success">编辑</el-button>
          <el-button type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="margin-top: 20px">
      <el-pagination
        v-model:current-page="pageFrom.pageNum"
        v-model:page-size="pageFrom.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加品牌" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="品牌名称"
            :label-width="formLabelWidth"
            prop="brandName"
          >
            <el-input v-model="form.brandName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="使用数量"
            :label-width="formLabelWidth"
            prop="brandUse"
          >
            <el-input v-model="form.brandUse" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="brandTop"
          >
            <el-input-number v-model="form.brandTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="brandStatus"
          >
            <el-radio-group v-model="form.brandStatus">
              <el-radio :value="0" size="large">显示</el-radio>
              <el-radio :value="1" size="large">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              确认
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import {
  brandList,
  addBrand,
  updateStatus,
  deleteBatchBrand,
} from "@/api/brand";
import { load } from "@amap/amap-jsapi-loader";
import { ElMessage } from "element-plus";
import { Row } from "element-plus/es/components/table-v2/src/components/index.mjs";
import type { TableInstance } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { it } from "node:test";
const multipleSelection = ref<any[]>([]);
const ruleFormRef = ref<FormInstance>();
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const tableData = ref([]);
const total = ref(0);
const pageFrom = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
});

const form = ref({
  brandName: null,
  brandStatus: null,
  brandTop: null,
  brandUse: null,
});
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
};
//批量删除
const ondeleteBatchBrand = () => {
  let ids: any = [];
  multipleSelection.value.forEach((item) => {
    ids.push(item.brandId);
  });
  if (ids == null || ids.length == 0) {
    ElMessage.warning("还未选择");
    return;
  }
  deleteBatchBrand(ids).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("删除成功");
      loadBrandList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
//添加校验参数
const rules = ref<FormRules>({
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  brandUse: [
    { required: true, message: "请输入品牌使用数量", trigger: "blur" },
  ],
  brandTop: [{ required: true, message: "请输入排序", trigger: "blur" }],
  brandStatus: [{ required: true, message: "请选择状态", trigger: "blur" }],
});

//校验  添加品牌
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addBrand(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadBrandList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
//修改状态
const onUpdateStatus = (row: any) => {
  updateStatus(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//保存品牌
const saveBrand = () => {
  addBrand(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      loadBrandList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
//添加框
const onAddBrand = () => {
  //清空数据
  form.value = {
    brandName: null,
    brandStatus: null,
    brandTop: null,
    brandUse: null,
  };
  dialogFormVisible.value = true;
};
//列表
const loadBrandList = () => {
  brandList(pageFrom.value).then((res) => {
    tableData.value = res.data.data.records;
    total.value = res.data.data.total;
  });
};
//每页条数
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  pageFrom.value.pageSize = val;
  loadBrandList();
};
//当前页数
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  pageFrom.value.pageNum = val;
  loadBrandList();
};
//钩子函数
onMounted(() => {
  loadBrandList();
});
</script>
